package org.wso2.carbon.registry.custom.dia.ui.utils;

import java.util.Scanner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.wso2.carbon.registry.common.ui.UIException;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.resource.ui.clients.CustomUIServiceClient;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletConfig;
import org.wso2.carbon.registry.custom.dia.ui.clients.TopicServiceClient;

public class AddDocumentUtil {

    private static final Log log = LogFactory.getLog(AddDocumentUtil.class);

    //TODO CHeck a solution for the redirection in dia_add_handler_ajaxprocessor.jsp
    //TODO do GET and SAVE docUtil  
    public static void addDocumentBean(
            HttpServletRequest request, ServletConfig config, HttpSession session) throws UIException {

        try {
            CustomUIServiceClient customUIServiceClient =
                    new CustomUIServiceClient(config, session);
 
            String parentPath = request.getParameter("parentPath");
            String documentName = request.getParameter("documentName");
            String title = request.getParameter("diaTitle");
            String summary = request.getParameter("diaSummary");
            String link = request.getParameter("diaLink");
            String relDoc1 = request.getParameter("diaRelDoc1");
            String tags = request.getParameter("diaTags");
             
            //TODO implement adding several related docs and tags.
            //TODO do Form Validation
            
            /*Structure of the OM
             *
             * <document>
                  <title>xxx</title>
                  <summary>xxxx</summary>
                  <externalLink>http://xxx</externalLink> 
             * </document>
             */
            
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMElement document = fac.createOMElement("document", null);
            OMElement titleElement = fac.createOMElement("title", null);
                titleElement.setText(title);
            OMElement summaryElement = fac.createOMElement("summary", null);
                summaryElement.setText(summary);
            OMElement linkElement = fac.createOMElement("externalLink", null);
                linkElement.setText(link);

            document.addChild(titleElement);
            document.addChild(summaryElement);
            document.addChild(linkElement); 
 
            String content = document.toString();

            customUIServiceClient.addTextContent(
                    parentPath, documentName, "dia", null, content);

        } catch (Exception e) {
            String msg = "Failed to add documentation. " + e.getMessage();
            log.error(msg, e);
            throw new UIException(msg, e);
        }
    }
/*
    public static void addAssociations(
            HttpServletRequest request,  ServletConfig config, HttpSession session) throws UIException{
        
    }
*/

    /**
     * Applies tags belongs to the current document by extracting tag details
     * from the diaAddFrom in dia_add_document.jsp. tags will be in Comma
     * separated String. So, first the tags are extracted and they are applied
     * using TopicServiceClient.
     *
     * @throws RegistryException
     *
     */
    public static void applyTags(
            HttpServletRequest request,  ServletConfig config, HttpSession session) throws UIException{

        try{
            TopicServiceClient client = new TopicServiceClient(config, session);
            String parentPath = request.getParameter("parentPath");
            String resourceName = request.getParameter("documentName");
            String sPath;

            if(!parentPath.equals("/") ){
                 sPath = parentPath+"/"+resourceName;
            } else{
                sPath = parentPath+resourceName;
            }

            String tags = request.getParameter("diaTags");
            Scanner sc = new Scanner(tags);
            sc.useDelimiter(",");

            String tag;
            while(sc.hasNext()){
                tag = sc.next();
                tag.trim();
                client.applyTag(sPath, tag);
            }
        }catch(Exception e){
            String msg = "Failed while adding tags. " + e.getMessage();
            log.error(msg, e);
            throw new UIException(msg, e);
        }
    }
}
